package com.game;


import android.content.Intent;
import android.database.Cursor;
import android.widget.Button;
import android.widget.EditText;

public class EditGameItemActivity extends EditTableActivity{

	private EditText gameIdEditText;
	private EditText choiceNameEditText;
	private EditText textPostEditText;
	private EditText textPreEditText;

	
	@Override
	protected void initalize(){
        setContentView(R.layout.editgameitem);
        
        SubmitButton = (Button)findViewById(R.id.gi_SubmitButton);
        SubmitButton.setOnClickListener(this);
        ViewButton = (Button)findViewById(R.id.gi_viewGameItemButton);
        ViewButton.setOnClickListener(this);
        HomeButton = (Button)findViewById(R.id.gi_homeButton);
        HomeButton.setOnClickListener(this);
        EditButton = (Button)findViewById(R.id.gi_editGameItemButton);
        EditButton.setOnClickListener(this);
        
    	gameIdEditText = (EditText)findViewById(R.id.gi_gameIdEditText);
		choiceNameEditText = (EditText)findViewById(R.id.gi_choiceNameEditText);
		textPostEditText = (EditText)findViewById(R.id.gi_textPostEditText);
		textPreEditText = (EditText)findViewById(R.id.gi_textPreEditText);
		
		ViewClass = ViewGameItemActivity.class;

	}
	
	@Override
    protected void save(){
    	
    	String gameIdStr = gameIdEditText.getText().toString();
    	String choiceName = choiceNameEditText.getText().toString();
    	String textPre = textPreEditText.getText().toString();
    	String textPost = textPostEditText.getText().toString();
    	int gameId = Integer.parseInt(gameIdStr);
    	GameItem newGame = new GameItem(gameId,choiceName,textPre,textPost);
    	myDBAdapter.insertGameItemData(newGame);
    	view();
    	
    	
    }
	
	@Override
	protected void edit() {
		startActivityForResult(new Intent(this,ListGameItemActivity.class),0);
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent)
	{  
		super.onActivityResult(requestCode, resultCode, intent);
		switch(requestCode)
		{
		case 0:
			if (resultCode == RESULT_OK && intent.hasExtra("ItemId"))
			{
			int _id = intent.getIntExtra("ItemId", -1);
			Cursor cursor = myDBAdapter.getGameItemByIdCursor(_id);
			cursor.moveToFirst();
			gameIdEditText.setText(cursor.getString(cursor.getColumnIndex("game_id")));
			choiceNameEditText.setText(cursor.getString(cursor.getColumnIndex("choice_name")));
			textPreEditText.setText(cursor.getString(cursor.getColumnIndex("text_pre")));
			textPostEditText.setText(cursor.getString(cursor.getColumnIndex("text_post")));
			}
			break;
		}
	}
	
	@Override
	public void onStart(){
		super.onResume();
		gameIdEditText.setText("");
		choiceNameEditText.setText("");
		textPreEditText.setText("");
		textPostEditText.setText("");
	}
}
